#!/bin/ash

if [ "$1" != "stop" ]; then
  exit 0
fi

# The upgrade system doesn't allow file removals. So use this script for this task.

#############
# FUNCTIONS #
#############

# doClean <file>
#   performs the file deletion
doClean() {
  if [ -e "$1" ]; then
    recallog -f init.log "Post-upgrade cleanup: deleting '$1'"
    mount -o remount,rw /
    rm -rf "$1"
    mount -o remount,ro /
  fi
}

########
# MAIN #
########

# V4.0.0 2016-01-29
doClean "/etc/init.d/S29recalboxsystem"
doClean "/etc/init.d/S42networkrestart"

# V4.1.0 2016-03-24
find "/usr/share/kodi/system/keymaps" -name "joystick.*.xml" |
  while read file; do
   doClean "${file}"
  done

# V4.1.0 2016-04-10
# the upgrade will fail to replace link by a file
doClean "/etc/network/interfaces.base"
doClean "/etc/wpa_supplicant/wpa_supplicant.conf"

# V4.1.0 2016-06-16
doClean "/etc/init.d/S10udev"
doClean "/etc/init.d/S30dbus"
doClean "/etc/init.d/S40network"
doClean "/etc/init.d/S45connman"

# V180209 2018-02-09
doClean "/etc/init.d/S46network-config"

# V180420 2018-04-20
doClean "/etc/init.d/S95api"

# V180627 2018-06-27
doClean "/etc/init.d/S04mountboot"

# V6.1 2019-09-XX
doClean "/etc/init.d/S02splash"
doClean "/etc/init.d/S03populate"
doClean "/etc/init.d/S04populate"
doClean "/etc/init.d/S05acpid"

# V6.1.1
doClean "/etc/init.d/S08connman"

# V6.x
doClean "/etc/init.d/S03splash"

exit 0
